<?php
class Pager {
    public static function getPageNav($recordCount, &$currentPage, $pageSize, $listSize = 7) {
        $pageCount = ceil($recordCount / $pageSize);
        if($currentPage > $pageCount) $currentPage = $pageCount;
        if( 0 == $currentPage) $currentPage = 1;
        
        $queryStr = array();
        foreach ($_GET as $key => $val) {
            if('page' != $key) {
                $queryStr[] = $key . '=' . urlencode($val);
            }
        }
        $scriptUrl = $_SERVER["SCRIPT_NAME"] . '?' . implode('&', $queryStr);
        
        $sideNum = ceil(($listSize - 1)/2);
        $preAdd = 0;
        $backAdd = 0;
        
        $pageStart = $currentPage - $sideNum;
        if($pageStart <= 1) {
            $pageStart = 1;
            $backAdd = $sideNum - ($currentPage - $pageStart);
        }

        $pageEnd = $currentPage + $sideNum;
        if($pageEnd >= $pageCount) {
            $pageEnd = $pageCount;
            $preAdd = $sideNum - ($pageEnd - $currrentPage);
        }
        
        
        if($preAdd > 0) $pageStart -= $preAdd;
        if($pageStart < 1) $pageStart = 1;
        if($backAdd > 0) $pageEnd += $backAdd;
        if($pageEnd > $pageCount) $pageEnd = $pageCount;
        
        if($pageEnd - $pageStart + 1 != $listSize) {
            $addNum = $listSize - ($pageEnd - $pageStart) - 1;
            if($pageStart == 1 && $pageEnd + $addNum <= $pageCount) {
                $pageEnd += $addNum;
            } else if($pageEnd == $pageCount && $pageStart - $addNum >= 1) {
                $pageStart -= $addNum;
            }
        }

        //$pageNav = '<script type="text/javascript">function goPage(){var url="' . $scriptUrl . '";var pageNum = parseInt(document.getElementById("goPageNum").value, 10);location=url + "&page="+pageNum}</script><style type="text/css">#listPageNav{float:right;} #listPageNav a{text-align:center;float:left;width:17px;height:17px;border:1px solid #555;background:#ddd;margin:2px;padding:1px;font-size:11px} #listPageNav a.current{border:1px solid blue;background:#999;color:red} #listPageNav input{text-align:center;border:1px solid #000; width:20px;height:18px;float:left;margin:2px}</style><div id="listPageNav"><div style="float:left;padding-top:3px;">记录总数：<b>' . $recordCount . '</b>条，每页显示<b>' . $pageSize . '</b>条, 分<b>' . $pageCount . '</b>页，当前第<b>' . $currentPage . '</b>页。</div><div style="float:left">';
        $pageNav = '<ul>';
        if($currentPage != 1) {
            //$pageNav .= '<li><a href="' . $scriptUrl . '&page=1' . '">|&lt;</a></li>';
            $pageNav .= '<li><a href="' . $scriptUrl . '&page=' . ($currentPage - 1) .  '">&lt;&lt</a><li>';
        }
        for ($i=$pageStart; $i<=$pageEnd; $i++ ) {
            $pageNav .= '<li><a href="' . $scriptUrl . '&page=' . $i . '"';
            if($i == $currentPage) {
                $pageNav .=' class="current"';
            }
            $pageNav .= '>' . $i . '</a></li>';
        }
        if($currentPage != $pageCount) {
            $pageNav .= '<li><a href="' . $scriptUrl . '&page=' . ($currentPage + 1) .  '">&gt;&gt;</a></li>';
            //$pageNav .= '<a href="' . $scriptUrl . '&page=' . $pageCount . '">&gt|</a>';
        }
        //$pageNav .='</div><div style="float:left;padding-top:3px"><span style="float:left;padding-top:2px;font-size:12px;margin-right:8px">&nbsp;&nbsp;跳转到第</span><input type="text" id="goPageNum" onkeyup="if(13 == event.keyCode)goPage()" value="' . $currentPage . '" /><input type="button" value="go" onclick="goPage()" style="height:20px;padding-bottom:3px"/></div></div>';
        $pageNav .= '</ul>';
        return $pageNav;
    }
};
?>